package com.itmk.classes.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.classes.entity.ClassEditVo;
import com.itmk.classes.entity.ClassParm;
import com.itmk.classes.entity.Classes;
import com.itmk.classes.service.ClassesService;
import com.itmk.classes.service.impl.ClassesServiceImpl;
import com.itmk.colloge.entity.Colloge;
import com.itmk.colloge.service.CollogeService;
import com.itmk.colloge.service.impl.CollogeServiceImpl;
import com.itmk.major.entity.Major;
import com.itmk.major.service.MajorService;
import com.itmk.major.service.impl.MajorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

/**
 * 处理班级模块的请求，班级控制器
 */
@WebServlet("/class.do")
public class ClassServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    private MajorService majorService = new MajorServiceImpl();
    private CollogeService collogeService = new CollogeServiceImpl();
    private ClassesService classesService = new ClassesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("classUI")) {
            classUI(req, resp);
        } else if (method.equals("addUI")) {
            addUI(req, resp);
        } else if (method.equals("getAllColloge")) {
            getAllColloge(req, resp);//查询所有的学院
        } else if (method.equals("getMajorList")) {
            getMajorList(req, resp);//查询所有的专业
        } else if (method.equals("addSave")) {
            addSave(req, resp); //新增班级
        } else if (method.equals("list")) {
            list(req, resp);
        } else if (method.equals("editUI")) {
            editUI(req, resp); //编辑页面
        }else if(method.equals("editSave")){
            editSave(req, resp);
        }else if(method.equals("delete")){
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数,删除的id
        String calssId = req.getParameter("calssId");
        //调用service
        classesService.delete(calssId);
        //告诉前端，后端删除成功过
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","删除成功");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递的参数
        String calssId = req.getParameter("calssId");
        String majorId = req.getParameter("majorId");
        int orderNum = Integer.valueOf(req.getParameter("orderNum"));
        String  className = req.getParameter("className");
        //封装参数
        Classes classes = new Classes();
        classes.setOrderNum(orderNum);
        classes.setMajorId(majorId);
        classes.setCalssName(className);
        classes.setCalssId(calssId);
        classesService.update(classes);
        //响应前端
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","编辑成功");
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject.toJSONString());
    }

    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传递过来的classId
        String calssId = req.getParameter("calssId");
        //获取要编辑的数据
        ClassEditVo classByClassId = classesService.getClassByClassId(calssId);
        req.setAttribute("classByClassId",classByClassId);
        //查询所有的学院
        List<Colloge> collogeAll = collogeService.getAll();
        req.setAttribute("collogeAll",collogeAll);
        //查询学院对应的专业
        List<Major> majorListClass = majorService.getMajorListBycollogeId(classByClassId.getCollogeId());
        req.setAttribute("majorListClass",majorListClass);
        //返回页面
        req.getRequestDispatcher(PREX + "/class_edit.jsp").forward(req, resp);
    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String className = req.getParameter("className");
        //组装参数
        ClassParm parm = new ClassParm();
        parm.setPage(page);
        parm.setLimit(limit);
        parm.setClassName(className);
        //表格数据
        List<Classes> list = classesService.list(parm);
        //查询总条数
        int count = classesService.getCount(parm);
        //组装返回值格式
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", list);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String className = req.getParameter("className");
        String majorId = req.getParameter("majorId");
        int orderNum = Integer.valueOf(req.getParameter("orderNum"));
        //封装参数
        Classes classes = new Classes();
        classes.setCalssId(UUID.randomUUID().toString().replaceAll("-",""));
        classes.setCalssName(className);
        classes.setMajorId(majorId);
        classes.setOrderNum(orderNum);
        classesService.add(classes);
        //告诉前端，后端操作成功
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "新增成功!");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject.toJSONString());
    }

    private void getMajorList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的学院id
        String collogeId = req.getParameter("collogeId");
        //根据学院id查询所有的专业
        List<Major> majorList = majorService.getMajorListBycollogeId(collogeId);
        //返回给前端
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "查询成功");
        object.put("data", majorList);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    /**
     * 查询所有的学院
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void getAllColloge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //所有的学院
        List<Colloge> collogeList = collogeService.getAll();
        //返回给前端
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "查询成功");
        object.put("data", collogeList);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    /**
     * 返回新增页面
     *
     * @param req
     * @param resp
     */
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/class_add.jsp").forward(req, resp);
    }

    /**
     * 返回班级管理页面
     *
     * @param req
     * @param resp
     */
    private void classUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/class.jsp").forward(req, resp);
    }

}
